package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeReplyEnrollService;
import com.bidevalution.business.service.BeReplyTitleService;
import com.bidevalution.business.service.BeSectionParticipantService;
import com.bidevalution.dao.entity.BeReplyTitle;
import com.bidevalution.dao.entity.BeSectionParticipant;
import com.bidevalution.support.context.Constants;
import com.qtp.core.model.Response;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: xiayf
 * @date: 2019-06-11 16:30
 * 操作答辩题目信息控制层
 */
@Log4j2
@RestController
@RequestMapping("/beReplyTitle")
public class BeReplyTitleController extends BaseController {

    @Autowired
    private BeReplyTitleService beReplyTitleService;
    @Autowired
    private BeReplyEnrollService beReplyEnrollService;
    @Autowired
    private BeSectionParticipantService beSectionParticipantService;

    /**
     * 保存答辩题目信息
     *
     * @param question
     * @param sectionId
     * @return
     */
    @RequestMapping(value = "/insertBeReplyTitle", method = RequestMethod.POST)
    @ResponseBody
    public Response insertBeReplyTitle(@RequestParam("question") String question, @RequestParam("sectionId") String sectionId,
                                       @RequestParam("creditCodes") String creditCodes, @RequestParam("isSend") String isSend,
                                        @RequestParam("identityTimeSetting") Integer identityTimeSetting, @RequestParam("replyTimeSetting") Integer replyTimeSetting) {
        try {
            if (StringUtils.isBlank(sectionId)) {
                return Response.fail("标段ID不可以为空");
            }
            //获取答辩题目信息
            BeReplyTitle beReplyTitle = beReplyTitleService.selectBySectionId(sectionId);
            boolean flag1 = false;
            //答辩进度，默认为1 答辩信息确认
            Integer state = Constants.ONE;
            //题目发送时间
            Date sendTime = null;
            //如果答辩题目为已发送状态 Y 则更新答辩进度为2 身份认证和保存发送时间
            if(Constants.STATE_Y.equals(isSend)){
                state = Constants.TWO;
                sendTime = DateUtil.now();
            }
            if (beReplyTitle == null) {
                beReplyTitle = new BeReplyTitle();
                //保存答辩题目信息
                beReplyTitle.setSeqId(IdsUtil.uuid());
                beReplyTitle.setSectionId(sectionId);
                beReplyTitle.setQuestion(question);
                beReplyTitle.setState(state);
                beReplyTitle.setIsSend(isSend);
                beReplyTitle.setSendTime(sendTime);
                beReplyTitle.setCreateTime(DateUtil.now());
                beReplyTitle.setUpdateTime(DateUtil.now());
                beReplyTitle.setIdentityTimeSetting(identityTimeSetting);
                beReplyTitle.setReplyTimeSetting(replyTimeSetting);
                flag1 = beReplyTitleService.insertBeReplyTitle(beReplyTitle);
            } else {
                //更新答辩题目信息
                beReplyTitle.setQuestion(question);
                beReplyTitle.setState(state);
                beReplyTitle.setIsSend(isSend);
                beReplyTitle.setSendTime(sendTime);
                beReplyTitle.setUpdateTime(DateUtil.now());
                beReplyTitle.setIdentityTimeSetting(identityTimeSetting);
                beReplyTitle.setReplyTimeSetting(replyTimeSetting);
                flag1 = beReplyTitleService.updateByReplyTitleIdSelective(beReplyTitle);
            }
            //查询报名单位信息
            List<BeSectionParticipant> participantList = beSectionParticipantService.getParticipantList(sectionId, null);
            //保存答辩单位信息的时候先删除其信息
            if (beReplyEnrollService.deleteByReplyId(beReplyTitle.getSeqId())) {
                log.error("************删除答辩单位信息失败********************");
            }
            //保存答辩单位信息
            boolean flag2 = beReplyEnrollService.insertBeReplyEnroll(participantList, beReplyTitle.getSeqId());
            //更新身份认证状态
            boolean flag3 = beReplyEnrollService.updateIdentityState(creditCodes, beReplyTitle.getSeqId());
            if (flag1 && flag2 && flag3) {
                return Response.ok(beReplyTitle.getSeqId());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("保存失败，请联系管理员！");
        }
        return Response.fail("保存失败");
    }

    /**
     * 查询题目相关信息
     * @param sectionId
     * @return
     */
    @RequestMapping(value = "/getBeReplyTitleInfo", method = RequestMethod.GET)
    @ResponseBody
    public Response<BeReplyTitle> getBeReplyTitleInfo(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beReplyTitleService.selectBySectionId(sectionId));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("查询题目相关信息失败");
        }
    }

    /**
     * 更新答辩进度
     *
     * @param seqId
     * @param state
     * @return
     */
    @RequestMapping(value = "/updateReplyState", method = RequestMethod.POST)
    @ResponseBody
    public Response updateReplyState(@RequestParam("seqId") String seqId, @RequestParam("state") Integer state) {
        try {
            if (StringUtils.isBlank(seqId)) {
                return Response.fail("ID不可以为空");
            }
            BeReplyTitle beReplyTitle = new BeReplyTitle();
            beReplyTitle.setSeqId(seqId);
            beReplyTitle.setState(state);
            beReplyTitle.setUpdateTime(DateUtil.now());
            if (beReplyTitleService.updateByReplyTitleIdSelective(beReplyTitle)) {
                return Response.ok("更新成功");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("更新失败，请联系管理员！");
        }
        return Response.fail("更新失败");
    }
}
